<template style="padding: 0">
  <div>
    <el-table
      :data="tableData"
      style="width: 100%"
      :header-cell-style="{ 'text-align': 'center' }"
      :cell-style="{ 'text-align': 'center' }"
      border
    >
      <el-table-column prop="uid" label="序号" width="200"> </el-table-column>
      <el-table-column prop="phone" label="用户号码"> </el-table-column>
      <el-table-column prop="pwd" label="密码"> </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.$index)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-button-group
      style="float: right; margin-right: 10%; border: 0; margin-top: 10px"
    >
      <el-button
        type="info"
        size="mini"
        plain
        @click="getUser(page--)"
        :disabled="isActive"
        >上一页</el-button
      >
      <el-button
        type="info"
        size="mini"
        plain
        @click="getUser(page++)"
        :disabled="disabled"
        >下一页</el-button
      >
    </el-button-group>
  </div>
</template>

<script>
import { CLOSING } from 'ws'

export default {
  data() {
    return {
      disabled: false,
      page: 1,
      tableData: [],
    }
  },
  mounted() {
    this.getUser(1)
  },

  methods: {
    // get 用户列表
    getUser() {
      this.axios.get(`/get_user?page=${this.page}`).then((res) => {
        if (res.data.data.length < 10) {
          this.disabled = true
        }
        this.tableData = res.data.data
        // console.log(this.tableData)
        this.tableData.forEach((item) => {
          // console.log(item)
          item.pwd = item.pwd.replace(/^(.).*(.)$/, "$1***$2")
        })
      })
    },
    //删除
    handleDelete(index) {
      let uid = this.tableData[index].uid;
      this.axios.delete(`/del_user/${uid}`).then(async (res) => {
        await this.getUser(this.page)
      });
    },
  },
  computed: {
    //当页码数到第一页时，上一页按钮禁用
    isActive() {
      this.disabled = false
      if (this.page > 1) {
        return false
      } else {
        return true
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.el-main.main {
  padding: 0;
}
</style>
